﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using The_Chronicles_of_Asku.Characters;

namespace The_Chronicles_of_Asku
{
    class Battle
    {
        Hero myHero = new Hero();
        Mob mob = new Mob();

        public void Fight(Hero myHero, Mob mob)
        {
            Console.WriteLine("You are facing a {0}, what do you want to do?", mob.Identifier);
            

            Console.WriteLine("Press enter to open up the battle.");
            Console.ReadLine();
            Console.Clear();

            do
            {
                string choice;

                do
                {
                    Console.Clear();
                    DisplayStats(myHero, mob);
                    Console.WriteLine("\n\n");
                    PrintChoices();
                    choice = Console.ReadLine();
                    Console.WriteLine();
                } while (choice != "1" && choice != "2" && choice != "3" && choice != "4");

                int charDamage, mobDamage, charMagic, charHeal, mobHeal;

                switch (choice)
                {
                    case "1":
                        string attackName = AttackChoices();
                        charDamage = myHero.getAttack(myHero);
                        mob.health -= charDamage;
                        if (mob.health < 0)
                        {
                            mob.health = 0;
                        }
                        Console.WriteLine("\nYou attack {0} with {1} for {2} damage. \n{0} now has {3} health left.", mob.Identifier, attackName, charDamage, mob.health);
                        break;
                        
                    case "2":
                        string magicName = MagicChoices();
                        charMagic = myHero.getMagic(myHero);
                        mob.health -= charMagic;
                        if (mob.health < 0)
                        {
                            mob.health = 0;
                        }
                        Console.WriteLine("\nYou attack {0} with {1} for {2} damage. \n{0} now has {3} health left.", mob.Identifier, magicName, charMagic, mob.health);
                        break;

                    case "3":
                        string healName = HealChoices();
                        charHeal = myHero.getHeal(myHero);
                        myHero.health += charHeal;
                        if (myHero.health > myHero.maxHealth)
                        {
                            myHero.health = myHero.maxHealth;
                        }
                        Console.WriteLine("\nYou heal yourself for {0} with {1}\n{2} now has {3} health left", charHeal, healName, myHero.Identifier, myHero.health);
                        break;

                    case "4":
                        myHero.hasFled = true;
                        Console.WriteLine("\nYou have fled.");
                        break;
                }

                if (myHero.health > 0 && mob.health > 0 && !myHero.hasFled)
                {
                    if (mob.health <= mob.healPoint)
                    {
                        mobHeal = mob.heal(mob);
                        mob.health += mobHeal;
                        if (mob.health > mob.maxHealth)
                        {
                            mob.health = mob.maxHealth;
                        }

                        Console.WriteLine("{0} healed itself for {1} points", mob.Identifier, mobHeal);
                    }

                    else
                    {
                        mobDamage = mob.getDamage(mob);
                        mobDamage = mobDamage - (myHero.armor * (int) .10);
                        myHero.health -= mobDamage;
                        if (myHero.health < 0)
                        {
                            myHero.health = 0;
                        }

                        Console.WriteLine("{0} attacked {1} for {2} points \n{1} has {3} health left", mob.Identifier, myHero.Identifier, mobDamage, myHero.health);
                    }

                    Console.ReadKey();
                    Console.Clear();
                }


            } while (myHero.health > 0 && mob.health > 0 && myHero.hasFled == false);

            Console.ReadKey();
            Console.Clear();

            if (myHero.health == 0)
            {
                myHero.isAlive = false;
                myHero.deathCount += 1;
            }

            if (myHero.hasFled == true)
            {
                myHero.isAlive = false;
                myHero.hasFled = false;
                myHero.health = 0;
            }
            
            if (myHero.health > 0)
            {
                myHero.gold = myHero.gold + mob.gold;
                if (myHero.gold >= myHero.goldCap)
                {
                    myHero.gold = myHero.goldCap;
                }
                myHero.xp = myHero.xp + (mob.xp * 2);
                myHero.GetPlayerLevel(myHero);

                Console.WriteLine("You now have {0} gold\n{1} xp\nYou're level {2}", myHero.gold, myHero.xp, myHero.level);
            }

            //mob.ResetStats(mob);
            //myHero.ResetStats(myHero);

            Console.WriteLine("Battle done.");
        }

        public static void PrintChoices()
        {
            Console.WriteLine(@"
1. Attack
2. Magic Attack
3. Heal
4. Flee
");
        }

        public static string AttackChoices()
        {
            Console.WriteLine(@"
1. Sword Swipe
2. Fury Strike
3. Lunge
4. Clean Attack
5. Martialed Strength
6. Low Cut");

            Console.WriteLine();
            string choice = Console.ReadLine();
            string attack = "";

            switch (choice)
            {
                case "1":
                    attack = "Sword Swipe";
                    break;
                case "2":
                    attack = "Fury Strike";
                    break;
                case "3":
                    attack = "Lunge";
                    break;
                case "4":
                    attack = "Clean Attack";
                    break;
                case "5":
                    attack = "Martialed Strength";
                    break;
                case "6":
                    attack = "Low Cut";
                    break;
            }

            return attack;
        }

        public static string MagicChoices()
        {
            Console.WriteLine(@"
1. Firebolt
2. Iceblast
3. Lightning Strike
4. Shadow Blast
5. Raging Inferno
6. Blinding Light");
            Console.WriteLine();
            string choice = Console.ReadLine();
            string magic = "";

            switch (choice)
            {
                case "1":
                    magic = "Firebolt";
                    break;
                case "2":
                    magic = "Iceblast";
                    break;
                case "3":
                    magic = "Lightning Strike";
                    break;
                case "4":
                    magic = "Shadow Blast";
                    break;
                case "5":
                    magic = "Raging Inferno";
                    break;
                case "6":
                    magic = "Blinding Light";
                    break;
            }

            return magic;
        }

        public static string HealChoices()
        {
            Console.WriteLine(@"
1. Holy Flash
2. Mending Light
3. Cleansing Draught
4. Reguvenating Words
5. Simple Bandaging
6. Healing Touch");
            Console.WriteLine();
            string choice = Console.ReadLine();
            string healChoice = "";

            switch (choice)
            {
                case "1":
                    healChoice = "Holy Flash";
                    break;
                case "2":
                    healChoice = "Mending Light";
                    break;
                case "3":
                    healChoice = "Cleansing Draught";
                    break;
                case "4":
                    healChoice = "Reguvenating Words";
                    break;
                case "5":
                    healChoice = "Simple Bandaging";
                    break;
                case "6":
                    healChoice = "Healing Touch";
                    break;
            }

            return healChoice;
        }

        public void DisplayStats(Hero myHero, Mob mob)
        {
            Console.WriteLine(@"
*************************************************************
Name:           Level:          Health:
-------------------------------------------------------------
{0}            {1}              {2}
-------------------------------------------------------------
{3}            {4}              {5}
*************************************************************
", myHero.Identifier, myHero.level, myHero.health, mob.Identifier, mob.level, mob.health);
        }
    }
}